﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

namespace CorporationWars
{
    public static class NewsFactory
    {
        enum NewsType : int
        {
            Local,
            Financial,
            Conflict,
            Crime,
            Traffic,
            CyberSpace,
            Bonus
        }
        enum NewsAlign : int
        {
            Good,
            Neutral,
            Bad
        }

        static Random rnd = new Random(DateTime.Now.Millisecond);

        public static NewsItem GenerateNewsItem(Game1 Game)
        {
            NewsItem retVal = new NewsItem(Game.getCyberDate());

            // get news type
            NewsType nt = (NewsType)Enum.Parse(typeof(NewsType), Enum.GetName(typeof(NewsType), (int)rnd.Next(0, 7)));

            // get news alignment
            NewsAlign gnb = (NewsAlign)Enum.Parse(typeof(NewsAlign), Enum.GetName(typeof(NewsAlign), (int)rnd.Next(0, 3)));
            
            int story;
            decimal pay = 0;
            int c = 0;
            Corporation corp = null;

            //nt = NewsType.Local;

            switch (nt)
            {
                #region Local
                case NewsType.Local:
                    story = rnd.Next(0, 5);
                    switch (gnb)
                    {
                        #region Bad
                        case NewsAlign.Bad:
                            story = rnd.Next(0, 5);
                            switch (story)
                            {
                                case 0:
                                    retVal.FullStory = string.Format(@"Due to solar storms, {0}'s serves are suffering a 
                                                                    degree of magnetic interference. This will result in
                                                                    a degree of network slow down during this cycle.", Game.CurrentSector.Name);
                                    retVal.HeadLine = string.Format("Solar Storms");
                                    retVal.TagLine = string.Format("{0} sufferes during solar storm...", Game.CurrentSector.Name);

                                    // Action slows down the sector movement.
                                    RenderStatic(Game, retVal, true);
                                    break;
                                case 1:
                                    retVal.FullStory = string.Format(@"{0} corporation based in {1} sector has been investigated
                                                        by global government agencies for withholding financial donations for the
                                                        last cycle. A spokesperson for {0} says that this is currently under investigation
                                                        and should be resolved in good time.", Game.playerCorp.Name, Game.playerCorp.SectorBase);
                                    retVal.HeadLine = string.Format("{0} Tax Debacle", Game.playerCorp.Name);
                                    retVal.TagLine = string.Format("Money owed to global govt from last cycle?");
                                    break;
                                case 2:
                                    retVal.FullStory = string.Format(@"{0} hardline struck by earth quake, traffic is being re routed 
                                                                but sector visibility will be effected during this cycle.", Game.CurrentSector.Name);
                                    retVal.HeadLine = "Earth Quakes";
                                    retVal.TagLine = string.Format("{0} shook by quakes..", Game.CurrentSector.Name);

                                    // Action required to render fog or texture overlay.
                                    AlterTanslationSpeed(Game, retVal, .05f);
                                    break;
                                case 3:
                                    retVal.FullStory = @"Staff moral is low, try poppig into H.R. and see if you can
                                                        set up an incentive scheme, or even pay rises may help";
                                    retVal.HeadLine = string.Format("{0} Staff Troubles", Game.playerCorp.Name);
                                    retVal.TagLine = "Staff moral low";

                                    // Action required staff moral..
                                    //retVal.Action = new NewsItemAction(AlterStaffMoral);
                                    AlterStaffMoral(Game, retVal, -.01f);
                                    break;
                                case 4:
                                    pay = (decimal)MathHelper.Lerp(5000, 50000, (float)rnd.NextDouble());
                                    retVal.FullStory = string.Format(@"A number of physical sites need repair and maintenance work done on them
                                                        costing a total of ${0:0.00}", pay);
                                    retVal.HeadLine = string.Format("{0} Maintenance Costs", Game.playerCorp.Name);
                                    retVal.TagLine = string.Format("${0:0.00} Maintenance Costs...", pay);

                                    //retVal.Action = new NewsItemAction(AlterFinances);
                                    AlterFinances(Game, retVal, -pay);
                                    break;
                            }
                            break;
                        #endregion
                        #region Good
                        case NewsAlign.Good:
                            story = rnd.Next(0, 5);
                            
                            switch (story)
                            {
                                case 0:
                                    retVal.FullStory = string.Format(@"Electrical storms have injected the {0}'s servers with extra vim.
                                                         Avatars may notice an increase in speed this cycle..", Game.CurrentSector.Name);
                                    retVal.HeadLine = "Electrical Storms";
                                    retVal.TagLine = string.Format("Electrical Surge's {0}", Game.CurrentSector.Name);

                                    AlterTanslationSpeed(Game, retVal, .2f);
                                    break;
                                case 1:
                                    pay = (decimal)MathHelper.Lerp(5000, 50000, (float)rnd.NextDouble());
                                    retVal.FullStory = string.Format(@"{0} of {1} sector has been awarded a ${2:0.00} tax
                                                        rebate by Global govt.", Game.playerCorp.Name, Game.playerCorp.SectorBase, pay);
                                    retVal.HeadLine = string.Format("{0} Tax Rebate", Game.playerCorp.Name);
                                    retVal.TagLine = "Tax is not a dirty word...";

                                    AlterFinances(Game, retVal, pay);
                                    break;
                                case 2:
                                    pay = (decimal)MathHelper.Lerp(.01f, 1f, (float)rnd.NextDouble());
                                    retVal.FullStory = string.Format("{0}'s of sector {1}, has had an unexpected share price spike, rising by {2}", Game.playerCorp.Name, Game.playerCorp.SectorBase, pay);
                                    retVal.HeadLine = string.Format("Shares Up!");
                                    retVal.TagLine = string.Format("{0}'s shares are up {1}", Game.playerCorp.Name, pay);

                                    AlterSharePrice(Game, retVal, pay);
                                    break;
                                case 3:
                                    retVal.FullStory = @"Staff moral is up, the staff seem to think you know what you are doing...";
                                    retVal.HeadLine = "Happy Staff";
                                    retVal.TagLine = "Staff moral up...";

                                    // Action required staff moral..
                                    AlterStaffMoral(Game, retVal, .01);
                                    break;
                                case 4:
                                    break;
                            }                            
                            break;
                        #endregion
                        #region Neutral
                        case NewsAlign.Neutral:
                            story = rnd.Next(0, 5);
                            c = rnd.Next(0, Game.CurrentSector.Corporations.Count);
                            corp = Game.CurrentSector.Corporations[c];

                            switch (story)
                            {
                                case 0:
                                    pay = (decimal)MathHelper.Lerp(5000, 50000, (float)rnd.NextDouble());
                                    retVal.FullStory = string.Format(@"{0} of {1} sector has been awarded a ${2:0.00} tax
                                                        rebate by Global govt.", corp.Name, corp.SectorBase, pay);
                                    retVal.HeadLine = string.Format("{0} Tax Rebate", corp.Name);
                                    retVal.TagLine = "Tax is not a dirty word...";
                                    corp.MakeTransaction(new FinancialTransaction() { Value = pay, By = retVal, Description = retVal.FullStory });
                                    break;
                                case 1:
                                    retVal.FullStory = string.Format(@"{0} corporation based in {1} sector has been investigated
                                                        by global government agencies for withholding financial donations for the
                                                        last cycle. A spokesperson for {0} says that this is currently under investigation
                                                        and should be resolved in good time.", corp.Name, corp.SectorBase);
                                    retVal.HeadLine = string.Format("{0} Tax Debacle", corp.Name);
                                    retVal.TagLine = string.Format("Money owed to global govt from last cycle");
                                    break;
                                case 2:
                                    pay = (decimal)MathHelper.Lerp(5000, 50000, (float)rnd.NextDouble());
                                    retVal.FullStory = string.Format(@"{0} of {1} sector has been found guilty of
                                                        tax fraud by global govt and fined ${2:0.00}", corp.Name, corp.SectorBase, pay);
                                    retVal.HeadLine = "Tax Fraud";
                                    retVal.TagLine = "Tax is a dirty word...";
                                    corp.MakeTransaction(new FinancialTransaction() { Value = -pay, By = retVal, Description = retVal.FullStory });
                                    break;
                                case 3:
                                    retVal.FullStory = string.Format("The 3 cycle maintenance project on {0} is now complete after 6 cycles, not bad, only 3 cycles late...", Game.CurrentSector.Name);
                                    retVal.HeadLine = "Hardline Maintenance";
                                    retVal.TagLine = "Hardline maintenance complete...";
                                    break;
                                case 4:
                                    retVal.FullStory = string.Format("We all know pinging a corperation is the start of a corperate attack, but this activity is noticably up last cycle based on ping levels in {0}", Game.Year - 1);
                                    retVal.HeadLine = "Corporate Ping detected";
                                    retVal.TagLine = "Don't ping off your neighbours...";
                                    break;
                            }
                            break;
                        #endregion
                    }
                    break;
                #endregion
                #region Bonus
                case NewsType.Bonus:
                    switch (gnb)
                    {
                        case NewsAlign.Bad:
                            story = rnd.Next(0, 3);
                            c = rnd.Next(0, Game.CurrentSector.Corporations.Count);
                            corp = Game.CurrentSector.Corporations[c];
                            switch (story)
                            {
                                case 0:
                                    pay = (decimal)MathHelper.Lerp(50000, 100000, (float)rnd.NextDouble());
                                    retVal.FullStory = string.Format(@"{0} of {1} sector has won ${2:0.00} in the corperate lotto, we think that
                                                    is going to help this corporation go from stength to strength!", corp.Name, corp.SectorBase, pay);
                                    retVal.HeadLine = "LOTTO WIN!";
                                    retVal.TagLine = string.Format("{0} of {1} wins {2:0.00}",corp.Name,corp.SectorBase,pay);
                                    corp.MakeTransaction(new FinancialTransaction() { By = retVal, Description = retVal.FullStory, Value = pay });
                                    break;
                                case 1:
                                    pay = (decimal)MathHelper.Lerp(25000, 50000, (float)rnd.NextDouble());
                                    retVal.FullStory = string.Format(@"A secret investor as invested ${2:0.00} in {0} of {1}.
                                                        No one knows whoe the investor is at this time, but the injection of
                                                        liquid assets at this time for this corporation is going to do no harm to the balance sheets.", corp.Name, corp.SectorBase, pay);
                                    retVal.HeadLine = "Secret Investor";
                                    retVal.TagLine = string.Format("{0} of {1} been given {2:0.00} by secret investor...",corp.Name,corp.SectorBase,pay);
                                    corp.MakeTransaction(new FinancialTransaction() { By = retVal, Description = retVal.FullStory, Value = pay });
                                    break;
                                case 2:
                                    retVal.FullStory = "bonus";
                                    retVal.HeadLine = "bonus";
                                    retVal.TagLine = "bonus...";
                                    break;
                            }
                            break;
                        case NewsAlign.Good:
                            break;
                        case NewsAlign.Neutral:
                            break;
                    }
                    retVal.FullStory = "bonus";
                    retVal.HeadLine = "bonus";
                    retVal.TagLine = "bonus...";
                    break;
                #endregion
                default:
                    story = rnd.Next(0, 5);
                    switch (story)
                    {
                        default:
                            retVal.FullStory = "Mock story goes here";
                            retVal.HeadLine = "Nothing Much happens";
                            retVal.TagLine = "Slow news day...";
                            break;
                    }
                    
                    break;
                    
            }

            return retVal;
        }

        public static void AlterFinances(Game1 Game, NewsItem newsItem, object value)
        {
            Game.playerCorp.MakeTransaction(new FinancialTransaction() { Value = (decimal)value, By = newsItem, Description = newsItem.FullStory });
        }
        public static void AlterStaffMoral(Game1 Game, NewsItem newsItem, object value)
        {
            Game.playerCorp.StaffMoral += float.Parse(value.ToString());

            if (Game.playerCorp.StaffMoral > 1)
                Game.playerCorp.StaffMoral = 1;

            if (Game.playerCorp.StaffMoral < 0)
                Game.playerCorp.StaffMoral = 0;
        }
        public static void AlterTanslationSpeed(Game1 Game, NewsItem newsItem, object value)
        {
            Game.CurrentSector.speedTran = (float)value;
        }
        public static void RenderStatic(Game1 Game, NewsItem newsItem, object value)
        {
            Game.CurrentSector.RenderStatic = (bool)value;
        }

        public static void AlterSharePrice(Game1 Game, NewsItem newsItem, object value)
        {
            Game.playerCorp.SharePrice += (decimal)value;
        }
    }
}
